<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="清关单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入清关单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号" prop="logisticsCode">
        <el-input
          v-model="queryParams.logisticsCode"
          placeholder="请输入物流单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="headId">
        <el-input
          v-model="queryParams.headId"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="清关结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择清关结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clearOrderList">
      <el-table-column type="index" label="序号" width="55" align="center">
        <template slot-scope="scope">
          {{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index +1}}
        </template>
      </el-table-column>
      <el-table-column label="清关单号" align="center" prop="orderCode">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleLook(scope.row)">{{ scope.row.orderCode }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="物流单编码" align="center" prop="logisticsCode" />
      <el-table-column label="负责人" align="center" prop="headStr" />
      <el-table-column label="清关结束时间" align="center" prop="endTime" width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="状态" align="center" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_clearance_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status === '0'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:clearOrder:edit']"
          >清关完成</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 清关完成对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="清关单号">
              {{form.orderCode}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物流单号">
              {{form.logisticsCode}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="负责人">
              {{form.headStr}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="清关结束时间">
              {{form.endTime}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="清关完成附件">
          <el-upload
            class="upload-demo"
            :headers="uploadAccessory.headers"
            :action="uploadAccessory.url"
            :on-remove="handleSuccessRemove"
            :on-success="handleSuccessSuccess"
            multiple
            drag
            accept=".jpg,.png,.jpeg,.pdf"
            :file-list="successFileList">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip text-center" slot="tip">
              <span>仅允许导入jpg/png/pdf格式文件。</span>
            </div>
          </el-upload>
        </el-form-item>
        <h1>商品信息</h1>
        <el-table :data="orderProductTable">
          <el-table-column label="序号" align="center" type="index" width="50"/>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="商品编码">
                      <span>{{ props.row.productCode }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="商品类型">
                      <span>{{ props.row.productTypeName }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="计数单位">
                      <span>{{ props.row.unitName }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="尺寸单位">
                      <span>{{ props.row.ccUnitName }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="尺寸">
                      <span v-if="props.row.longWith || props.row.weight || props.row.height">{{ props.row.longWith + '*' + props.row.weight + '*' + props.row.height }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="重量">
                      <span>{{ props.row.zl }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="交货期（厂家）">
                      <span>{{ props.row.manufacturerDelivery }}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="交货期（买家）">
                      <span>{{ props.row.delivery }}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="详情">
                  <span>{{ props.row.detail }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
          <el-table-column label="商品型号" align="center" prop="modal" :show-overflow-tooltip="true"/>
          <el-table-column label="生产厂家" align="center" prop="scPurchaseCompanyName"/>
          <el-table-column label="商品数量" align="center" prop="productNum"/>
          <el-table-column label="成本" align="center" prop="oldOnlyPrice"/>
          <el-table-column label="单价" align="center" prop="onlyPrice"/>
          <el-table-column label="总价" align="center" prop="allPrice"/>
          <el-table-column label="状态" align="center" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_order_product_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-loading="btnLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 物流详情对话框 -->
    <el-dialog title="清关详情" :visible.sync="openLook" width="80%" append-to-body>
      <el-form>
        <el-form-item label="物流单号" >
          {{orderCode}}
        </el-form-item>

        <el-form-item label="清关附件" v-if="fileList.length > 0">
          <el-upload
            action=""
            class="upload-demo"
            :disabled="true"
            :on-preview="handlePreview"
            :file-list="fileList">
          </el-upload>
        </el-form-item>

        <el-form-item label="清关完成附件" v-if="successFileList.length > 0">
          <el-upload
            action=""
            class="upload-demo"
            :disabled="true"
            :on-preview="handlePreview"
            :file-list="successFileList">
          </el-upload>
        </el-form-item>
      </el-form>
      <h1>商品信息</h1>
      <el-table :data="orderProductTable">
        <el-table-column label="序号" align="center" type="index" width="50"/>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="商品编码">
                    <span>{{ props.row.productCode }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="商品类型">
                    <span>{{ props.row.productTypeName }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="计数单位">
                    <span>{{ props.row.unitName }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="尺寸单位">
                    <span>{{ props.row.ccUnitName }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="尺寸">
                    <span v-if="props.row.longWith || props.row.weight || props.row.height">{{ props.row.longWith + '*' + props.row.weight + '*' + props.row.height }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="重量">
                    <span>{{ props.row.zl }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="交货期（厂家）">
                    <span>{{ props.row.manufacturerDelivery }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="交货期（买家）">
                    <span>{{ props.row.delivery }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="详情">
                <span>{{ props.row.detail }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
        <el-table-column label="商品型号" align="center" prop="productName" :show-overflow-tooltip="true"/>
        <el-table-column label="生产厂家" align="center" prop="scPurchaseCompanyName"/>
        <el-table-column label="商品数量" align="center" prop="productNum"/>
        <el-table-column label="成本" align="center" prop="oldOnlyPrice"/>
        <el-table-column label="单价" align="center" prop="onlyPrice"/>
        <el-table-column label="总价" align="center" prop="allPrice"/>
        <el-table-column label="状态" align="center" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.wms_order_product_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listClearOrder, getClearOrder, updateClearOrder } from "@/api/wms/clearOrder";
import {getToken} from "@/utils/auth";

export default {
  name: "ClearOrder",
  dicts: ['wms_clearance_status', 'wms_order_product_status'],
  data() {
    return {
      prefix: process.env.VUE_APP_BASE_API,
      // 附件参数
      uploadAccessory: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/wms/clearOrder/uploadPic"
      },
      //按钮加载
      btnLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 清关订单管理表格数据
      clearOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openLook: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        logisticsCode: null,
        headId: null,
        endTime: null,
        status: null
      },
      fileList: [],
      successFileList: [],
      // 清关完成附件
      successAccessoryList: [],
      orderProductTable: [],
      orderCode: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询清关订单管理列表 */
    getList() {
      this.loading = true;
      listClearOrder(this.queryParams).then(response => {
        this.clearOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.successAccessoryList = []
      this.successFileList = []
      this.orderProductTable = []
      this.form = {
        orderCode: null,
        logisticsCode: null,
        headStr: null,
        endTime: null,
        successAccessoryId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderCode = row.orderCode
      getClearOrder(orderCode).then(response => {
        let data = response.data;
        this.form.orderCode = data.orderCode;
        this.form.logisticsCode = data.logisticsCode;
        this.form.headStr = data.headStr;
        this.form.endTime = data.endTime;
        this.orderProductTable = data.products
        this.open = true;
        this.title = "清关完成";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.successAccessoryList.length > 0) {
        this.btnLoading = true
        this.form.products = this.orderProductTable
        this.form.successAccessoryList = this.successAccessoryList
        updateClearOrder(this.form).then(response => {
          this.$modal.msgSuccess("提交成功，等待审核");
          this.open = false;
          this.getList();
        }).finally(f => {
          this.btnLoading = false
        });
      } else {
        this.$message.error("请上次清关完成附件！")
      }
    },
    // 查看物流商品
    handleLook(row){
      this.fileList = []
      this.successFileList = []
      const orderCode = row.orderCode
      this.orderCode = orderCode
      getClearOrder(orderCode).then(response => {
        let data = response.data;
        this.orderProductTable = data.products
        let accessoryList = data.accessoryList
        let successAccessoryList = data.successAccessoryList

        if (accessoryList && accessoryList.length > 0) {
          accessoryList.forEach(item => {
            let urls = item.split("/")
            this.fileList.push({name: urls[urls.length - 1], url: this.prefix + item})
          })
        }
        if (successAccessoryList && successAccessoryList.length > 0) {
          successAccessoryList.forEach(item => {
            let urls = item.split("/")
            this.successFileList.push({name: urls[urls.length - 1], url: this.prefix + item})
          })
        }

        this.openLook = true
      });
    },
    // 文件点击事件
    handlePreview(file){
      window.open(file.url,'_blank');
    },
    // 清关完成附件删除
    handleSuccessRemove(file, fileList) {
      this.successAccessoryList.splice(this.successAccessoryList.indexOf(file.url), 1);
    },
    // 清关完成附件上传成功处理
    handleSuccessSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.successAccessoryList.push(response.imgUrl)
      }
    },
  }
};
</script>
